package repository

import (
	"fmt"
	"hcy-api/structs/idp"
	"hcy-api/structs/tables"
	"idp/global"
)

var ClientRepository = new(clientRepository)

type clientRepository struct{}

func (r clientRepository) Save(u tables.IdpClient) (err error) {
	return global.DB.Save(&u).Error
}

func (r clientRepository) FindProbableList(bios, boardSerial, diskSerial, mac, hostname string) (li []tables.IdpClient, err error) {
	err = global.DB.Where("bios_serial = ? or main_board_serial = ? or hard_disk_serial = ? "+
		" or active_mac= ? or hostname = ? ",
		bios, boardSerial, diskSerial, mac, hostname).Find(&li).Error
	return
}

func (r clientRepository) FindAll() (li []tables.IdpClient, err error) {
	err = global.DB.Where("id >0 ").Find(&li).Error
	return
}

func (r clientRepository) List(uid int64, accountLv int) (li []tables.IdpClient, err error) {
	if accountLv == tables.AccountLvForRoot {
		err = global.DB.Model(&tables.IdpClient{}).Where("id >0").Find(&li).Error
	} else {
		err = global.DB.Model(&tables.IdpClient{}).Where("id in ( select client_id from idp_map_branch_client where branch_id in ( select branch_id from idp_map_branch_user where uid = ?) )", uid).Error
	}
	return
}

func (r clientRepository) FindPage(uid int64, in idp.SearchClientPage) (li []tables.IdpClient, total int64, err error) {
	tx := global.DB.Model(&tables.IdpClient{})
	if in.BranchId != 0 {
		tx.Where(" id in (select client_id from idp_map_branch_client where branch_id = ?)  ", in.BranchId)
	} else {
		tx.Where(" id in ( select client_id from idp_map_branch_client where branch_id in ( select branch_id from idp_map_branch_user where uid = ?) ) ", uid)
	}
	tx.Where(" hostname like ? ", fmt.Sprintf("%%%s%%", in.Hostname))
	tx.Where(" ip like ? ", fmt.Sprintf("%%%s%%", in.Ip))
	tx.Count(&total).Offset((in.PageNum - 1) * in.PageSize).Limit(in.PageSize).Find(&li)
	err = tx.Error
	return
}

func (r clientRepository) FindPageForRoot(in idp.SearchClientPage) (li []tables.IdpClient, total int64, err error) {
	tx := global.DB.Model(&tables.IdpClient{})
	if in.BranchId != 0 {
		tx.Where(" id in (select client_id from idp_map_branch_client where branch_id = ?)  ", in.BranchId)
	}
	tx.Where(" hostname like ? ", fmt.Sprintf("%%%s%%", in.Hostname))
	tx.Where(" ip like ? ", fmt.Sprintf("%%%s%%", in.Ip))
	tx.Count(&total).Offset((in.PageNum - 1) * in.PageSize).Limit(in.PageSize).Find(&li)
	err = tx.Error
	return
}

// FreeList 获取未分配的设备列表
func (r clientRepository) FreeList(in idp.SearchClientPage) (li []tables.IdpClient, total int64, err error) {
	tx := global.DB.Model(&tables.IdpClient{})
	tx.Where(" id not in ( select distinct(client_id) from idp_map_branch_client ) ")
	tx.Where(" hostname like ? ", fmt.Sprintf("%%%s%%", in.Hostname))
	tx.Where(" ip like ? ", fmt.Sprintf("%%%s%%", in.Ip))
	tx.Count(&total).Offset((in.PageNum - 1) * in.PageSize).Limit(in.PageSize).Find(&li)
	err = tx.Error
	return

}

func (r clientRepository) CountClientNum() (num int64, err error) {
	err = global.DB.Raw("select count(*) from idp_client ").Find(&num).Error
	return
}
